package com.itboye.shop.me.qianbao;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.app.MyApplication;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.MoneyShengyuBean;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.IsUtilUid;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 我的钱包
 */
public class MyQianBaoActivity extends BaseActivity implements Observer{
    TextView tvTitle;
    ImageView back;
    TextView tvTest;

    LinearLayout qianbao_main;
    TextView qianbao_line;
    TextView qianbao_money, qianbao_zhanghu;
    RelativeLayout qianbao_chongzhi, qianbao_tixian, qianbao_zhangdan, qianbao_yinhangka;

    RelativeLayout layout;
    PopupWindow mPopWin;
    YuEPresenter presenter;
    MoneyShengyuBean bean;
    RelativeLayout titlebar;
    private Dialog dialog;// 自定义Dialog
    EditText dialog_et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_qian_bao);
        tvTitle.setText("我的钱包");
        tvTest.setVisibility(View.VISIBLE);
        tvTest.setText("设置密码");

        presenter=new YuEPresenter(this);

        showExtraDialog();
    }
    @Override
    protected void onStart() {
        super.onStart();
        presenter.onMoney(IsUtilUid.isUid());
    }
    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tvTest:// 设置支付密码
                intent = new Intent(this, ZhiFuPassActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_chongzhi://充值
                intent = new Intent(this, ChongZhiActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_tixian://提现
                dialog.show();
                break;
            case R.id.qianbao_zhangdan://账单
                intent = new Intent(this, JiaoYiJiLuActivity.class);
                startActivity(intent);
                break;
            case R.id.qianbao_yinhangka://绑定银行卡
                intent = new Intent(this, MyZhangHuActivity.class);
                intent.putExtra("type","qianbao");
                startActivity(intent);
                break;
            default:
                break;
        }
    }
    /**
     * 自定义Dialog(输入支付密码)
     */
    private void showExtraDialog() {
        dialog = new Dialog(MyQianBaoActivity.this, R.style.rel_dialog);
        View view = LayoutInflater.from(MyQianBaoActivity.this).inflate(
                R.layout.dialog_normal_layout, null);
        dialog.setContentView(view);
        dialog_et = (EditText) view.findViewById(R.id.dialog_et);

        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        user_title.setText("输入您的6位支付密码");
        dialog_et.setText("");

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        final String rele = (String) SPUtils.get(MyApplication.instatnce, null, Const.ZHIFUMIMA, "");
        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(dialog_et.getText().toString().equals("")){
                    MAlert.alert("请输入支付密码",Gravity.CENTER);
                }else {
                    showProgressDialog("验证中..", true);
                    presenter.onZhiFuPassYanZheng(IsUtilUid.isUid(), dialog_et.getText().toString().trim());
                }
            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == YuEPresenter.Yue_success) {
                bean = (MoneyShengyuBean) entity.getData();
                Log.d("byaltitledatae", bean.getTotal());
                qianbao_money.setText("" + Double.parseDouble(bean.getTotal()) / 100 + "元");
                double in = Double.parseDouble(bean.getTotal());
                SPUtils.put(MyQianBaoActivity.this, null,Const.QIAN, in+"");
            }
            if (entity.getEventType() == YuEPresenter.Yue_fail) {
                MAlert.alert(entity.getData());
            }
            if (entity.getEventType() == presenter.yanzheng_pass_success) {
                MAlert.alert(entity.getData(),Gravity.CENTER);
                Intent intent = new Intent(MyQianBaoActivity.this, TiXianActivity.class);
                startActivity(intent);
                dialog.dismiss();
            }
            if (entity.getEventType() == presenter.yanzheng_pass_fail) {
                MAlert.alert(entity.getData(),Gravity.CENTER);
            }
        }
        try{
            closeProgressDialog();
        }catch (Exception e){
        }
    }
}
